# -*- coding: utf-8 -*-
"""
产品模块序列化器
定义产品相关模型的序列化规则
"""

from rest_framework import serializers
from django.contrib.auth import get_user_model
from product.models import Product, ProductStatus
from utils.serializers import CustomModelSerializer

User = get_user_model()


class ProductSerializer(CustomModelSerializer):
    """
    产品信息 序列化器，用于产品信息的序列化与反序列化
    自动处理创建人和修改人字段
    """
    status = serializers.ChoiceField(choices=ProductStatus.choices, help_text="货品状态")
    unit_rate = serializers.JSONField(required=False, help_text="计量单位转换率")
    sku_supplier = serializers.CharField(required=False, help_text="供应商SKU")
    barcode = serializers.CharField(required=False, allow_blank=True, help_text="条形码")
    inventory_method = serializers.CharField(required=False, help_text="库存方法")
    costing_method = serializers.CharField(required=False, help_text="成本计算方法")
    is_batch_tracked = serializers.BooleanField(required=False, help_text="是否批次跟踪")
    is_serial_tracked = serializers.BooleanField(required=False, help_text="是否序列号跟踪")
    shelf_life_days = serializers.IntegerField(required=False, help_text="保质期天数")
    safety_stock = serializers.IntegerField(required=False, help_text="安全库存")
    reorder_point = serializers.IntegerField(required=False, help_text="再订货点")
    max_stock = serializers.IntegerField(required=False, help_text="最大库存")
    default_warehouse_id = serializers.IntegerField(required=False, help_text="默认仓库ID")
    default_location = serializers.CharField(required=False, help_text="默认库位")
    preferred_supplier_id = serializers.IntegerField(required=False, help_text="首选供应商ID")
    purchaser_id = serializers.IntegerField(required=False, help_text="采购员ID")
    lead_time = serializers.IntegerField(required=False, help_text="提前期")
    moq = serializers.IntegerField(required=False, help_text="最小起订量")
    last_purchase_price = serializers.DecimalField(max_digits=14, decimal_places=4, required=False, help_text="最近采购价")
    list_price = serializers.DecimalField(max_digits=14, decimal_places=4, required=False, help_text="列表价")
    standard_cost = serializers.DecimalField(max_digits=14, decimal_places=4, required=False, help_text="标准成本")
    current_cost = serializers.DecimalField(max_digits=14, decimal_places=4, required=False, help_text="当前成本")
    income_account_id = serializers.IntegerField(required=False, help_text="收入科目ID")
    cogs_account_id = serializers.IntegerField(required=False, help_text="销售成本科目ID")
    asset_account_id = serializers.IntegerField(required=False, help_text="资产科目ID")
    tax_code = serializers.CharField(required=False, help_text="税码")
    version = serializers.CharField(required=False, allow_blank=True, help_text="版本号")
    
    class Meta:
        model = Product
        fields = '__all__'
        read_only_fields = ['create_time', 'update_time', 'product_id']


class ProductSimpleSerializer(serializers.ModelSerializer):
    """产品简要信息序列化器"""
    class Meta:
        model = Product
        fields = ['product_id', 'sku', 'name', 'spc', 'unit_pr']


class UserSimpleSerializer(serializers.ModelSerializer):
    """用户简要信息序列化器"""
    class Meta:
        model = User
        fields = ['id', 'username', 'first_name', 'last_name']